<template>
	<view style="background-color: #f2f2f2;width: 100%;height: 100%;">
		<eui-nav-bar title="修改密码" color="#000" rightIcon="" backgroundColor="#fff" />
		<view style="padding: 0 20rpx;">
			<view class="user-img">
				<image :src="personImg" @error="setErrorImg" mode="scaleToFill"
					style="width: 144rpx;height: 144rpx;border-radius: 50%;" class="margin-top-40"></image>
				<view class="font-size-36 margin-top-20">{{mainStore.loginInfo.sUserName}}</view>
			</view>
			<view class="change-title">修改密码</view>
		</view>
		<eui-card class="padding-left-28" leftPadding="28rpx" rightPadding="0px">
			<eui-card-input v-model="mainStore.loginInfo.sUserName" title="用户名" placeholder="" type="text" displayable
				alignType="left" hPadding="0" style="padding-right: 10px;" />
			<eui-card-input v-model="c_old_password" title="原密码" placeholder="原密码" type="password" alignType="left" hPadding="0" style="padding-right: 10px;"/>
			<eui-card-input v-model="c_new_password" title="新密码" placeholder="新密码" type="password" alignType="left" hPadding="0" style="padding-right: 10px;"/>
			<eui-card-input v-model="c_affirm_password" title="确认密码" placeholder="确认密码" type="password" alignType="left" :showBottomLine="false" hPadding="0" style="padding-right: 10px;"/>
		</eui-card>
		<view class="user-footer">
			<eui-button type="primary" @click="UpdatePassWord" title="提交" singleTag="primary_主按钮" />
		</view>
	</view>
</template>

<script setup type="ts">
	import { onLoad } from '@dcloudio/uni-app';
	import { ref } from 'vue'
	import { useMainStore } from '../../../store/modules/store';
	import { useLoginStore } from '../../../store/modules/useLoginStore';
	import service from '../../../common/service';
	const mainStore = useMainStore();
	const LoginStore = useLoginStore();
	
	const Info = ref({})
	const imgUrl = LoginStore.EnjoyMain.replace('/Enjoy/Service', '');
	const personImg = ref(imgUrl + '/File/DownLoadImage/' + mainStore.loginInfo.sUser);
	const c_old_password = ref("")
	const c_new_password = ref("")
	const c_affirm_password = ref("")
	
	const setErrorImg = () =>{
		personImg.value =Info.value.c_sex=='男'||Info.value.c_sex=='M'?'/static/img/avatar_male.png':'/static/img/avatar_female.png';
	}
	
	const UpdatePassWord = () =>{
		console.log("c_old_password.value: " + JSON.stringify(c_old_password.value));
		if(!c_old_password.value){
			service.enjoyShowToast('请先输入原密码!')
			return false;
		}
		if(!c_new_password.value){
			service.enjoyShowToast('请先输入新密码!')
			return false;
		}else{
			if(c_new_password.value.length<6)
			{
				service.enjoyShowToast('密码长度最小是6!')
				return false;
			}
			if(!c_affirm_password.value){
				service.enjoyShowToast('请再次输入新密码!')
				return false;
			}
		}
		if(c_new_password.value && c_affirm_password.value)
		{
			if(c_new_password.value!=c_affirm_password.value)
			{
				service.enjoyShowToast('两次密码输入不一致,请重新输入密码!')
			   return false;
			}
		}
		service.enjoyShowLoading('正在修改...');
		let request = {
		    url: LoginStore.EnjoyMain,
		    data: {
		        UniqueKey: "管易通40主页",
		        ObjectData: {
					sName:mainStore.loginInfo.sUser,//mainStore.loginInfo.sUser,
					sType :'捷诚零售业系统',
					sPassType:'登录密码',
					sPassword:btoa(c_new_password.value),
					sOldPswd:btoa(c_old_password.value)
				},
		        MethodName: "UpdatePassWord",
		    },
		}
		service.request(request, (res) => {
		    if (res && res.data) {
		        if (!res.data.HasException) {
					if(res.data.ObjectData){
						uni.showToast({
						    duration: 2000,
						    icon: 'none',
						    title: '登录密码修改成功，请重新登录!'
						});
						setTimeout(()=>{
							plus.runtime.restart();
						},2000);
					}
		        } else {
		            service.enjoyShowToast(res.data.Exception.Message)
		        }
		    }
			service.enjoyHideLoading();
		}, () => {
			service.enjoyHideLoading();
		    console.log('失败')
		});
	}
	const GetMyInfoList = ()=>{
		service.enjoyShowLoading('正在加载...');
		let request = {
		    url: LoginStore.EnjoyMain,
		    data: {
		        UniqueKey: "管易通40主页",
		        ObjectData: mainStore.loginInfo.sUser,
		        MethodName: "GetMyInfoList",
		    },
		}
		service.request(request, (res) => {
		    if (res && res.data) {
		        if (!res.data.HasException) {
					const ObjectData = res.data.ObjectData;
					if(ObjectData.length>0){
						Info.value = {
							...ObjectData[0],
						};
					}
		        } else {
		            service.enjoyShowToast(res.data.Exception.Message)
		        }
		    }
			service.enjoyHideLoading();
		}, () => {
			service.enjoyHideLoading();
		    console.log('失败')
		});
	}
	onLoad((option)=>{
		//如果是登录界面上初始密码修改过来的则不需要获取信息了
		if(!(option && option.nagviFlag == 'login')){
			GetMyInfoList();
		}		
	})
</script>

<style lang="scss">
	.user-img {
		background-color: #fff;
		height: 282rpx;
		width: 100%;
		border-radius: 20rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-top: 20rpx;
	}

	.margin-top-40 {
		margin-top: 40rpx;
	}

	.margin-top-20 {
		margin-top: 20rpx;
	}

	.font-size-36 {
		font-size: 36rpx;
	}

	.change-title {
		color: rgba(136, 136, 136, 1);
		font-size: 26rpx;
		padding: 20rpx 0 0 0;
	}

	.user-footer {
		position: fixed;
		z-index: 9;
		bottom: 0;
		left: 0;
		right: 0;
		height: 100rpx;
		background-color: #fff;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	.padding-left-28 {
		padding-left: 28rpx !important;
	}
</style>